from sqlalchemy.orm import Session
from . import models, schemas
from .security import get_password_hash  # 使用新的安全模块

def get_user(db: Session, user_id: int):
    return db.query(models.User).filter(models.User.id == user_id).first()

def get_user_by_username(db: Session, username: str):
    return db.query(models.User).filter(models.User.username == username).first()

def get_user_by_email(db: Session, email: str):
    return db.query(models.User).filter(models.User.email == email).first()

def get_users(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.User).offset(skip).limit(limit).all()

def create_user(db: Session, user: schemas.UserCreate):
    hashed_password = get_password_hash(user.password)
    db_user = models.User(
        username=user.username,
        email=user.email,
        hashed_password=hashed_password,
        is_active=user.is_active,
        is_superuser=user.is_superuser
    )
    db.add(db_user)
    db.commit()
    db.refresh(db_user)
    return db_user

def get_scripts(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.Script).offset(skip).limit(limit).all()

def get_user_scripts(db: Session, user_id: int, skip: int = 0, limit: int = 100):
    return db.query(models.Script).filter(models.Script.owner_id == user_id).offset(skip).limit(limit).all()

def get_script(db: Session, script_id: int):
    return db.query(models.Script).filter(models.Script.id == script_id).first()

def create_script(db: Session, script: schemas.ScriptCreate, user_id: int):
    db_script = models.Script(**script.dict(), owner_id=user_id)
    db.add(db_script)
    db.commit()
    db.refresh(db_script)
    return db_script

def update_script(db: Session, script_id: int, script: schemas.ScriptUpdate):
    db_script = db.query(models.Script).filter(models.Script.id == script_id).first()
    if db_script:
        update_data = script.dict(exclude_unset=True)
        for field, value in update_data.items():
            setattr(db_script, field, value)
        db.commit()
        db.refresh(db_script)
    return db_script

def delete_script(db: Session, script_id: int):
    db_script = db.query(models.Script).filter(models.Script.id == script_id).first()
    if db_script:
        db.delete(db_script)
        db.commit()
    return db_script

def create_execution_log(db: Session, execution_log: schemas.ExecutionLogCreate, user_id: int):
    db_execution_log = models.ExecutionLog(**execution_log.dict(), user_id=user_id)
    db.add(db_execution_log)
    db.commit()
    db.refresh(db_execution_log)
    return db_execution_log

def get_execution_logs(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.ExecutionLog).offset(skip).limit(limit).all()

def get_user_execution_logs(db: Session, user_id: int, skip: int = 0, limit: int = 100):
    return db.query(models.ExecutionLog).filter(models.ExecutionLog.user_id == user_id).offset(skip).limit(limit).all()

def get_script_execution_logs(db: Session, script_id: int, skip: int = 0, limit: int = 100):
    return db.query(models.ExecutionLog).filter(models.ExecutionLog.script_id == script_id).offset(skip).limit(limit).all()

def update_execution_log(db: Session, log_id: int, execution_log: schemas.ExecutionLogUpdate):
    db_execution_log = db.query(models.ExecutionLog).filter(models.ExecutionLog.id == log_id).first()
    if db_execution_log:
        update_data = execution_log.dict(exclude_unset=True)
        for field, value in update_data.items():
            setattr(db_execution_log, field, value)
        db.commit()
        db.refresh(db_execution_log)
    return db_execution_log

def get_execution_log(db: Session, log_id: int):
    return db.query(models.ExecutionLog).filter(models.ExecutionLog.id == log_id).first()
